define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/post/index' + location.search,
                    add_url: 'user/post/add',
                    edit_url: 'user/post/edit',
                    del_url: 'user/post/del',
                    multi_url: 'user/post/multi',
                    import_url: 'user/post/import',
                    table: 'post',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'staff_name', title: __('Staff_name'), operate: 'LIKE'},
                        {field: 'height', title: __('Height')},
                        {field: 'weight', title: __('Weight')},
                        {field: 'age', title: __('Age')},
                        {field: 'is_share', title: __('Is_share'), searchList: {"0":__('Is_share 0'),"1":__('Is_share 1')}, formatter: Table.api.formatter.normal},
                        {field: 'figure', title: __('Figure'), operate: 'LIKE'},
                        {field: 'character', title: __('Character'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'evaluate', title: __('Evaluate'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'value', title: __('Value')},
                        {field: 'check_state', title: __('Check_state'), searchList: {"0":__('Check_state 0'),"1":__('Check_state 1'),"2":__('Check_state 2')}, formatter: Table.api.formatter.normal},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    title: __('转为花名册'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    text: '转为花名册',
                                    confirm: '确认转为花名册？',
                                    url: 'staff/staff/setstaff',
                                    success: function (data, ret) {
                                        Layer.alert('成功');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        $(".btn-refresh").trigger("click");
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        $(".btn-refresh").trigger("click");
                                        return false;
                                    },visible: function (row) {
                                        console.log('rowrow',row);

                                        if (row.check_state == 1 && row.is_share == 0){
                                                return true;
                                            }
                                            return false;
                                        }
                                    },
                                // {
                                //     name: 'detail',
                                //     title: __('转为技师'),
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     icon: 'fa fa-list',
                                //     url: 'staff/staff/setpackage',
                                //     callback: function (data) {
                                //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //     }
                                // }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/post/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/post/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'user/post/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
